package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.dao.IResumeSendDao;
import com.jiangyao.manager.IntegralManager;
import com.jiangyao.model.dto.InterviewInvitationDetailDTO;
import com.jiangyao.model.entity.InterviewInvitation;
import com.jiangyao.dao.IInterviewInvitationDao;
import com.jiangyao.model.entity.ResumeSend;
import com.jiangyao.model.vo.InterviewInvitationVo;
import com.jiangyao.model.vo.UserSessionVO;
import com.jiangyao.service.InterviewInvitationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 面试邀请 服务实现类
 * </p>
 *
 * @author zhoushuangxiang
 * @since 2020-08-13
 */
@Service
public class InterviewInvitationServiceImpl extends BaseServiceImpl<IInterviewInvitationDao, InterviewInvitation> implements InterviewInvitationService {
    @Autowired
    private IInterviewInvitationDao iInterviewInvitationDao;

    @Autowired
    private IResumeSendDao iResumeSendDao;

    @Autowired
    private IntegralManager integralManager;

    @Override
    public InterviewInvitationVo getInterviewInvitationDetail(Integer sendId) {
        UserSessionVO apiLoginSession = getApiLoginSession();
        Integer userId = apiLoginSession.getUserId();
        InterviewInvitationVo interviewInvitationDetail = iInterviewInvitationDao.getInterviewInvitationDetail(sendId, userId);
        return interviewInvitationDetail;
    }

    @Override
    public void sendInvitation(InterviewInvitation interviewInvitation) throws ServerException{
        try {
            QueryWrapper<InterviewInvitation> interviewInvitationQueryWrapper = new QueryWrapper<>();
            interviewInvitationQueryWrapper.eq("org_id",interviewInvitation.getOrgId())
                    .eq("send_id",interviewInvitation.getSendId())
                    .eq("user_id",interviewInvitation.getUserId());
            InterviewInvitation interviewInvitation1 = baseMapper.selectOne(interviewInvitationQueryWrapper);
            //如果不存在， 则添加
            if(null == interviewInvitation1){
                baseMapper.insert(interviewInvitation);
                //修改状态
                QueryWrapper<ResumeSend> resumeSendQueryWrapper = new QueryWrapper<>();
                resumeSendQueryWrapper.eq("send_id",interviewInvitation.getSendId());
                ResumeSend resumeSend = iResumeSendDao.selectOne(resumeSendQueryWrapper);
                resumeSend.setDeliveryStatus(2);
                iResumeSendDao.update(resumeSend,resumeSendQueryWrapper);
                // 首次则需要记录积分流水并添加积分
                integralManager.saveIntegralLogForinterviewInvitation(interviewInvitation);
            }else {
                //如果存在， 那么可能是为修改，
                interviewInvitation.setId(interviewInvitation1.getId());
                baseMapper.updateById(interviewInvitation);
            }
        } catch (Exception e) {
            throw new ServerException(101,"发送面试邀请失败");
        }

    }
}
